import CryptoJS from 'crypto-js';
const encrypt = (word, keyStr) => {
    keyStr = keyStr ? keyStr : 'cUJXHTUw5uDPZGi5ICxIGsgQ'; //判断是否存在ksy，不存在就用定义好的key
    const key = CryptoJS.enc.Utf8.parse(keyStr);
    const srcs = CryptoJS.enc.Utf8.parse(word);
    const iv = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
    const ivStr= String.fromCharCode(...iv)
    const ivHex=CryptoJS.enc.Utf8.parse(ivStr)
    const encrypted = CryptoJS.AES.encrypt(srcs, key, { mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7, iv: ivHex});
    return encrypted.toString();
}

const decrypt = (word, keyStr) => {
    keyStr = keyStr ? keyStr : "cUJXHTUw5uDPZGi5ICxIGsgQ"
    const key = CryptoJS.enc.Utf8.parse(keyStr)
    const iv = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
    const ivStr= String.fromCharCode(...iv)
    const ivHex=CryptoJS.enc.Utf8.parse(ivStr)
    const decrypt = CryptoJS.AES.decrypt(word, key, {
        mode: CryptoJS.mode.CBC,
        padding: CryptoJS.pad.Pkcs7,
        iv: ivHex
    })
    return CryptoJS.enc.Utf8.stringify(decrypt).toString()
}
const md5 = (word) => {
    return CryptoJS.MD5(word).toString()
}
const time = () => {
    return Math.round(new Date().getTime()/1000).toString()
}
export { encrypt,decrypt, md5, time }